<?php
require_once('internal/Engine.base.php');

class Admin extends Engine {
	protected $module, $moduleName, $sections, $menu;
	public $info;
	function __construct() {
		$this->menu = array();
		$this->head = new Head;
		$this->head->add('meta',array('http-equiv' => 'Content-type','content' => 'text/html; charset=utf-8'));

		$this->initModule();
		$this->loadInfo();
	}
	/*
	 These functions are inherited from the Engine class and are not needed in
	 an admin engine
	*/
	protected function fetchModules() {}
	protected function initModules() {}

	/*
	 function initModule([string $module])
	 Loads and initializes the chosen module
	*/
	protected function initModule() {
		// Checking if a module name is given, if not, then setting if from the GET
		if (func_num_args()<1) $module = $_GET['action'];
		else $module = func_get_arg(0);

		$do = trim($module) ? trim($module) : 'menu';
		$path = checkIfExists($do);
		if (!$path) error('admin/errors/action');
		require_once($path.'module.php');
		$this->module = new $do($this);
		$this->moduleName = $do;

		if (isset($this->module->customTitle)) $this->title = $this->module->customTitle;
		$this->sections = isset($this->module->sections) ? $this->module->sections : array();
	}

	/*
	 function loadInfo()
	 Fetches the module's info from the database
	*/
	protected function loadInfo() {
		global $db;
		$db->modeAssoc();
		$q = $db->select('modules', '*', array("shortname='{$this->moduleName}'"));

		$this->info = $q->fields;
	}

	/*
	 function content()
	 Executes the module and displays its output
	*/
	protected function content() { $this->module->execute(); }

	/*
	 function render()
	 Handles the AJAX functionality and renders the whole page
	*/
	public function render() {
		global $mae, $lang;
		if (engine_type($mae['site_engine'])=='admin') {
			parent::siteTop();
			echo $lang['admin']['errors']['security'];
			parent::siteBottom();
		} else {
			$req = ($_SERVER['HTTP_X_REQUESTED_WITH'] != 'XMLHttpRequest');
			if ($req) $this->siteTop();
			$this->content();
			if ($req) $this->siteBottom();
		}
	}

	/*
	 function displayFooter()
	 Displays the standard administration panel footer
	*/
	protected function displayFooter() {
		echo '<p>'.MAE_PRODUCT_NAME.'</p>';
		echo '<p>&copy; 2007 <b>mundus</b>novis studio</p>';
		// echo '<p><a href="admin.php?action=update">Updates available</a></p>';
	}

	/*
	 function displaySections()
	 Displays links to various sections of the current module
	*/
	protected function displaySections() {
		echo '<ul id="sections">';
		while (list($name,$link)=each($this->sections)) {
			echo '<li><a href="'.$link.'"><span><span>'.$name.'</span></span></a></li>';
		}
		echo '</ul>';
	}

	/*
	 function displayMenu()
	 Displays the main menu
	*/
	protected function displayMenu() {
		global $db;
		$db->modeAssoc();
		$q = $db->select('modules', '*', array("type='adminmodule'"));
		echo '<ul id="mainmenu">';
		foreach ($q as $k => $r) {
			if (($r['shortname']!='config') && ($this->moduleName!=$r['shortname'])) {
				$path = checkIfExists($r['shortname']);
				if ($path) {
					$link = "admin.php?action=".$r['shortname'];
					$name = empty($lang[$r['shortname']]['name']) ? $r['fullname'] : $lang[$r['shortname']]['name'];
					$this->menu[$name] = $link;
					echo "<li><a href=\"$link\"><span><span>$name</span></span></a></li>";
				}
			}
		}
		echo '</ul>';
	}
}
?>
